package com.quercus.test;

import com.quercus.dao.CustomerDao;
import com.quercus.dao.UserDao;
import com.quercus.dao.impl.CustomerDaoImpl;
import com.quercus.dao.impl.UserDaoImpl;
import com.quercus.entity.Customer;
import com.quercus.entity.User;
import com.quercus.util.XmlUtil;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * @author quercus
 */
public class MainFrame {
    public static void main(String[] args) {
        System.out.println("-----欢迎访问尚学堂CRM系统-----");
        //先登录
        System.out.println("-----请先登录-----");
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("用户编号：");
            String userId = input.next();
            System.out.println("密码：");
            String password = input.next();
            UserDao userDao = XmlUtil.getInstance("userDao");
            User user = userDao.findUser(userId, password);
            if (user == null) {
                System.out.println("登录失败，请重新登录");
            } else {
                //登录成功，退出循环
                break;
            }
        }
        //登录成功显示主菜单
        showMainMenu();
    }

    private static void showMainMenu() {
        Scanner input = new Scanner(System.in);
        while (true) {
            //1.显示主菜单
            System.out.println("--------尚学堂CRM系统--------");
            System.out.println("       1.查询客户列表");
            System.out.println("       2.查询指定编号客户");
            System.out.println("       3.添加新客户");
            System.out.println("       4.修改客户");
            System.out.println("       5.删除客户");
            System.out.println("       6.分配客户");
            System.out.println("       7.退出");
            System.out.println("-------------- -------------");
            //2.从键盘输入选择项
            System.out.println("请选择：");
            int choice = input.nextInt();
            //3.根据选择项输出相应语句
            switch (choice) {
                case 1:
                    System.out.println("【客户端关系系统】----查询客户列表");
                    findAll();
                    break;
                case 2:
                    System.out.println("【客户端关系系统】----查询指定编号客户");
                    findById();
                    break;
                case 3:
                    System.out.println("【客户端关系系统】----添加新客户");
                    save();
                    break;
                case 4:
                    System.out.println("【客户端关系系统】----修改客户");
                    update();
                    break;
                case 5:
                    System.out.println("【客户端关系系统】----删除客户");
                    delete();
                    break;
                case 6:
                    System.out.println("【客户端关系系统】----分配客户");
                    updateUserId();
                    break;
                case 7:
                    System.out.println("谢谢使用");
                    return;
                default:
                    System.out.println("输入错误！");
            }
        }
    }

    private static void findAll() {
        //调用后台获取所有客户
        CustomerDao customerDao = XmlUtil.getInstance("customerDao");
        List<Customer> customerList = customerDao.findAll();
        //在前台输出所有客户
        customerList.forEach(System.out::println);
    }

    private static void findById() {
        //输入要查询的员工编号
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要查询的客户编号");
        String cno = input.next();
        //调用后台获取指定客户
        CustomerDao customerDao = XmlUtil.getInstance("customerDao");
        Customer customer = customerDao.findById(cno);
        //在前台输出客户
        if (customer != null) {
            System.out.println(customer);
        } else {
            System.out.println("查无此客户");
        }
    }

    private static void save() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入客户编号：");
        String cno = input.nextLine();
        System.out.println("请输入客户姓名：");
        String name = input.nextLine();
        System.out.println("请输入客户性别：");
        String sex = input.nextLine();
        System.out.println("请输入客户地址：");
        String address = input.nextLine();
        System.out.println("请输入客户出生日期(yyyy-MM-dd)：");
        Date birthDate = Date.valueOf(input.nextLine());
        System.out.println("请输入用户的联系方式：");
        String phones = input.nextLine();
        List<String> phoneList = new ArrayList<>();
        Collections.addAll(phoneList, phones);
        System.out.println("请输入客户的备注信息：");
        String remark = input.nextLine();
        System.out.println("请输入客户的级别：");
        int level = input.nextInt();
        Customer customer = new Customer(cno, name, sex, birthDate, address, phoneList, remark, null, level, 0, null);
        CustomerDao customerDao = XmlUtil.getInstance("customerDao");
        try {
            customerDao.save(customer);
            System.out.println("添加客户成功");
        } catch (RuntimeException e) {
            System.out.println("添加客户失败");
        }
    }

    private static void update() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入客户编号：");
        String cno = input.nextLine();
        System.out.println("请输入客户地址：");
        String address = input.nextLine();
        System.out.println("请输入备注信息：");
        String remark = input.nextLine();
        System.out.println("请输入客户等级：1 普通客户2银质客户 3黄金客户4钻石客户 ");
        int level = input.nextInt();
        Customer customer = new Customer(cno, address, remark, level);
        CustomerDao customerDao = XmlUtil.getInstance("customerDao");
        try {
            customerDao.update(customer);
            System.out.println("修改客户成功");
        } catch (RuntimeException e) {
            System.out.println("修改客户失败");
        }
    }

    private static void delete() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入客户编号：");
        String cno = input.nextLine();
        CustomerDao customerDao = XmlUtil.getInstance("customerDao");
        try {
            customerDao.delete(cno);
            System.out.println("删除客户成功");
        } catch (RuntimeException e) {
            System.out.println("删除客户失败");
        }
    }

    private static void updateUserId() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入客户编号：");
        String cno = input.next();
        System.out.println("请输入要分配给的员工编号：");
        String userId = input.next();
        CustomerDao customerDao = XmlUtil.getInstance("customerDao");
        try {
            customerDao.updateUserId(cno, userId);
            System.out.println("分配客户成功");
        } catch (RuntimeException e) {
            System.out.println("分配客户失败");
        }
    }
}
